package com.orangelife.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.orangelife.exception.AppBizException;

/**
 * 验证手机、邮箱、用戶名等格式
 * 
 * @author zhr
 * 
 */
public class CheckMobileAndEmail {
	
	public static void main(String[] args) throws AppBizException {
		
		String latitude = "177591465121";
		System.out.println(CheckMobileAndEmail.isMobileNO(latitude));

	}
	
	
	/**
	 * 验证邮箱地址是否正确
	 * 
	 * @param email
	 * @return
	 * @throws AppBizException 
	 */
	public static boolean checkEmail(String email) throws AppBizException {
		boolean flag = false;
		try {
			if(email.length()>64){
				throw new AppBizException(6, "邮箱地址长度超出");
			}
			String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
			Pattern regex = Pattern.compile(check);
			Matcher matcher = regex.matcher(email);
			flag = matcher.matches();
			if(!flag){
				throw new AppBizException(2008, "邮箱地址格式错误");
			}
		} catch (Exception e) {
			flag = false;
			throw new AppBizException(2008, "邮箱地址格式错误");
		}
		return flag;
	}
	
	/**
	 * 验证邮政编码 
	 * 
	 * @param email
	 * @return
	 * @throws AppBizException 
	 */
	public static boolean checkPost(String post) throws AppBizException {
		boolean flag = false;
		try {
			String check = "[1-9]\\d{5}(?!\\d)";
			Pattern regex = Pattern.compile(check);
			Matcher matcher = regex.matcher(post);
			flag = matcher.matches();
			if(!flag){
				throw new AppBizException(2008, "邮政编码格式错误");
			}
		} catch (Exception e) {
			flag = false;
			throw new AppBizException(2008, "邮政编码格式错误");
		}
		return flag;
	}

	/**
	 * 验证手机号码
	 * 
	 * @param mobiles
	 * @return [0-9]{5,9}
	 * @throws AppBizException 
	 */
	public static boolean isMobileNO(String mobiles) throws AppBizException {
		boolean flag = false;
		try {
			String mobile = ProConfig.getProValues("mobile");
			Pattern p = Pattern
					.compile(mobile);
			Matcher m = p.matcher(mobiles);
			flag = m.matches();
			if(!flag){
				throw new AppBizException(2002, "手机格式错误");
			}
		} catch (Exception e) {
			flag = false;
			throw new AppBizException(2002, "手机格式错误");
		}
		return flag;
		
	}
	
	/**
	 * 验证手机号码(返回布尔类型)
	 * 
	 * @param mobiles
	 * @return [0-9]{5,9}
	 * @throws AppBizException 
	 */
	public static boolean isMobileT(String mobiles) throws AppBizException {
		boolean flag = true;
		try {
			String mobile = ProConfig.getProValues("mobile");
			Pattern p = Pattern
					.compile(mobile);
			Matcher m = p.matcher(mobiles);
			flag = m.matches();
		} catch (Exception e) {
			flag = false;
		}
		return flag;
		
	}
	
	/**
	 * 验证电话号码
	 * 
	 * @param mobiles
	 * @return [0-9]{5,9}
	 * @throws AppBizException 
	 */
	public static boolean isTelNO(String mobiles) throws AppBizException {
		boolean flag = false;
		try {
			String tel = ProConfig.getProValues("tel");
			Pattern p = Pattern
					.compile(tel);
			Matcher m = p.matcher(mobiles);
			flag = m.matches();
			if(!flag){
				throw new AppBizException(2002, "电话格式错误");
			}
		} catch (Exception e) {
			flag = false;
			throw new AppBizException(2002, "电话格式错误");
		}
		return flag;
		
	}
	
	/**
	 * 验证电话或手机号码
	 * 
	 * @param mobiles
	 * @return [0-9]{5,9}
	 * @throws AppBizException 
	 */
	public static boolean isTelOrMobileNO(String mobiles) throws AppBizException {
		boolean flag = false;
		try {
			String telOrMobile = ProConfig.getProValues("telOrMobile");
			Pattern p = Pattern
					.compile(telOrMobile);
			/*Pattern p = Pattern
					.compile("^(1[3,5,8,7]{1}[\\d]{9})|(((400)-(\\d{3})-(\\d{4}))|^((\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})|(\\d{4}|\\d{3})-(\\d{3,7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1})|(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1}))$)$");
			*/
			Matcher m = p.matcher(mobiles);
			flag = m.matches();
			System.out.println();
			if(!flag){
				throw new AppBizException(2002, "电话或手机格式错误");
			}
			flag=true;
		} catch (Exception e) {
			
			throw new AppBizException(2002, "电话或手机格式错误");
		
		}
		return flag;
		
	}
	
	/**
	 * 验证电话或邮箱
	 * 
	 * @param mobiles
	 * @return [0-9]{5,9}
	 * @throws AppBizException 
	 */
	public static boolean telOrEmail(String mobiles,String error) throws AppBizException {
		boolean flag = false;
		try {
			String telOrMobile = ProConfig.getProValues("telOrEmail");
			Pattern p = Pattern
					.compile(telOrMobile);
			Matcher m = p.matcher(mobiles);
			flag = m.matches();
			System.out.println();
			if(!flag){
				throw new AppBizException(1003, error+"格式错误");
			}
			flag=true;
		} catch (Exception e) {
			
			throw new AppBizException(1003, error+"格式错误");
		
		}
		return flag;
		
	}
	 

	/**
	 * 
	 * 校验是否全是数字
	 * @param number
	 * @return
	 */
	public static boolean isNum(String number) {
		boolean flag = false;
		try {
			Pattern p = Pattern.compile("^[0-9]+$");
			Matcher m = p.matcher(number);
			flag = m.matches();
			
		} catch (Exception e) {
			flag = false;
		}
		return flag;
	}
	
	/**
	 * 校验真实姓名是否全是中文
	 * @param number
	 * @return
	 * @throws AppBizException 
	 */
	public static boolean isChinese(String trueName) throws AppBizException {
		boolean flag = false;
		try {
			Pattern p = Pattern.compile("[\u4E00-\u9FA5]+");
			Matcher m = p.matcher(trueName);
			flag = m.matches();
			 if (flag) {  
		            int strLenth = getStrLength(trueName);  
		            if (strLenth < 1 ) {  
		            	throw new AppBizException(7, "输入参数真实姓名长度不足");
		            }  
		            if( strLenth > 20){
		            	throw new AppBizException(6, "输入参数真实姓名长度超出");
		            }
		        } 
			 if(!flag){
				 throw new AppBizException(2009, "真实姓名格式错误");
				}
		} catch (Exception e) {
			throw new AppBizException(2009, "真实姓名格式错误");
		}
		return flag;
	}
	
	/**
	 * 校验真实姓名是否全是中文(返回布尔)
	 * @param number
	 * @return
	 * @throws AppBizException 
	 */
	public static boolean isChineseT(String trueName) throws AppBizException {
		boolean flag = true;
		try {
			Pattern p = Pattern.compile("[\u4E00-\u9FA5]+");
			Matcher m = p.matcher(trueName);
			flag = m.matches();
			 if(!flag){
				 flag = false;
				
				}
		} catch (Exception e) {
			 flag = false;
		}
		return flag;
	}
	
	/** 
     * 获取字符串的长度，对双字符（包括汉字）按两位计数 
     *  
     * @param value 
     * @return 
     */  
    public static int getStrLength(String value) {  
        int valueLength = 0;  
        String chinese = "[\u0391-\uFFE5]";  
        for (int i = 0; i < value.length(); i++) {  
            String temp = value.substring(i, i + 1);  
            if (temp.matches(chinese)) {  
                valueLength += 2;  
            } else {  
                valueLength += 1;  
            }  
        }  
        return valueLength;  
    }  
	
	
    /** 
     * 验证用户名,不支持中文，支持英文（包括全角字符）、数字、下划线和减号 ,长度为1-20位
     *  
     * @param userName 
     * @return 
     * @throws AppBizException 
     */  
    public static boolean validateUserName(String userName) throws AppBizException {  
        String validateStr = "^[\\w\\-－＿[0-9]]+$";  
        boolean rs = false;  
        rs = matcher(validateStr, userName);  
        if (rs) {  
            int strLenth = getStrLength(userName);  
            if (strLenth < 1 ) {  
            	throw new AppBizException(7, "输入参数用户名长度不足");
            }  
            if( strLenth > 20){
            	throw new AppBizException(6, "输入参数用户名长度超出");
            }  
        }else{
        	throw new AppBizException(2009, "用戶名格式错误");
        }
        return rs;  
    }  
    
    /** 
     * 验证参数，支持中英文（包括全角字符）、数字、下划线和减号 （全角及汉字算两位）,长度为1-20位,中文按二位计数 
     *  
     * @param userName 
     * @return 
     * @throws AppBizException 
     */  
    public static boolean validateNickName(String userName) throws AppBizException {  
        String validateStr = "^[\\w\\-－＿[0-9]\u4e00-\u9fa5\uFF21-\uFF3A\uFF41-\uFF5A]+$";  
        boolean rs = false;  
        rs = matcher(validateStr, userName);  
        if (rs) {  
            int strLenth = getStrLength(userName);  
            if (strLenth < 1 ) {  
            	throw new AppBizException(7, "内置错误：输入参数昵称长度不足");
            }  
            if( strLenth > 20){
            	throw new AppBizException(6, "内置错误：输入参数昵称长度超出");
            }  
        }else{
        	throw new AppBizException(2009, "输入参数昵称格式错误");
        }
        return rs;  
    }
    
    /** 
     * 验证内容，支持中英文（包括全角字符）、数字、下划线和减号 ,长度不限，自己判断
     *  
     * @param userName 
     * @return 
     * @throws AppBizException 
     */  
    public static boolean validateString(String userName,String errorName) throws AppBizException {  
        String validateStr = "^[\\w\\-－＿[0-9]\u4e00-\u9fa5\uFF21-\uFF3A\uFF41-\uFF5A #()（）\\[\\]\\{\\}\\【\\】ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫ]+$";
        boolean rs = false;  
        rs = matcher(validateStr, userName);  
        if (!rs) {  
        	throw new AppBizException(3, errorName+"参数格式错误");
        }
        return rs;  
    }
    
    /** 
     * 验证内容，支持中英文（包括全角字符）、数字、下划线和减号 ,.，。长度不限，自己判断
     *  
     * @param userName 
     * @return 
     * @throws AppBizException 
     */  
    public static boolean validateContent(String userName,String errorName) throws AppBizException {  
        String validateStr = "^[\\w\\-－＿,.，。[0-9]\u4e00-\u9fa5\uFF21-\uFF3A\uFF41-\uFF5A]+$";  
        boolean rs = false;  
        rs = matcher(validateStr, userName);  
        if (!rs) {  
        	throw new AppBizException(3, "输入"+errorName+"参数格式错误");
        }
        return rs;  
    }
    
    /** 
     * 验证用户名,不支持中文，支持英文（包括全角字符）、数字、下划线和减号 ,长度为36位
     *  
     * @param userName 
     * @return 
     * @throws AppBizException 
     */  
    public static boolean validateUUID(String uuid, String field) throws AppBizException {  
        String validateStr = "[A-Za-z0-9-]+";  
        boolean rs = false;  
        rs = matcher(validateStr, uuid);  
        if (rs) {  
            int strLenth = getStrLength(uuid);  
            if (strLenth < 36 ) {  
            	throw new AppBizException(7, "输入参数" + field +"长度不足");
            }  
            if( strLenth > 36){
            	throw new AppBizException(6, "输入参数" + field +"长度超出");
            }  
        }else{
        	throw new AppBizException(2009, field +"格式错误");
        }
        return rs;  
    } 
    
    /** 
     * 验证用户名,不支持中文，支持英文（包括全角字符）、数字、下划线和减号 ,长度为36位
     *  
     * @param userName 
     * @return 
     * @throws AppBizException 
     */  
    public static boolean validatePmID(String pmID) throws AppBizException {  
        String validateStr = "[A-Za-z0-9-]+";  
        boolean rs = false;  
        rs = matcher(validateStr, pmID);  
        if (rs) {  
            int strLenth = getStrLength(pmID);  
            if (strLenth < 36 ) {  
            	throw new AppBizException(7, "输入参数pmID长度不足");
            }  
            if( strLenth > 36){
            	throw new AppBizException(6, "输入参数pmID长度超出");
            }  
        }else{
        	throw new AppBizException(2009, "pmID格式错误");
        }
        return rs;  
    } 
    
    /** 
     * 验证用户名,不支持中文，支持英文（包括全角字符）、数字、下划线和减号 ,长度为36位
     *  
     * @param userName 
     * @return 
     * @throws AppBizException 
     */  
    public static boolean validateRoleID(String roleID) throws AppBizException {  
        String validateStr = "[A-Za-z0-9-]+";  
        boolean rs = false;  
        rs = matcher(validateStr, roleID);  
        if (rs) {  
            int strLenth = getStrLength(roleID);  
            if (strLenth < 36 ) {  
            	throw new AppBizException(7, "输入参数roleID长度不足");
            }  
            if( strLenth > 36){
            	throw new AppBizException(6, "输入参数roleID长度超出");
            }  
        }else{
        	throw new AppBizException(2009, "roleID格式错误");
        }
        return rs;  
    } 
    
    /** 
     * 验证用户名,不支持中文，支持英文（包括全角字符）、数字、下划线和减号 ,长度为36位
     *  
     * @param userName 
     * @return 
     * @throws AppBizException 
     */  
    public static boolean validatePmMemberID(String pmMemberID) throws AppBizException {  
        String validateStr = "[A-Za-z0-9-]+";  
        boolean rs = false;  
        rs = matcher(validateStr, pmMemberID);  
        if (rs) {  
            int strLenth = getStrLength(pmMemberID);
            if (strLenth < 36 ) {  
            	throw new AppBizException(7, "输入参数memberID长度不足");
            }  
            if( strLenth > 36){
            	throw new AppBizException(6, "输入参数memberID长度超出");
            }  
        }else{
        	throw new AppBizException(2009, "memberID格式错误");
        }
        return rs;  
    } 
    
    /** 
     * 验证用户名,不支持中文，支持英文（包括全角字符）、数字、下划线和减号 ,长度为36位
     *  
     * @param userName 
     * @return 
     * @throws AppBizException 
     */  
    public static boolean validateCommID(String commID) throws AppBizException {  
        String validateStr = "[A-Za-z0-9-]+";  
        boolean rs = false;  
        rs = matcher(validateStr, commID);  
        if (rs) {  
            int strLenth = getStrLength(commID);  
            if (strLenth < 36 ) {  
            	throw new AppBizException(7, "输入参数commID长度不足");
            }  
            if( strLenth > 36){
            	throw new AppBizException(6, "输入参数commID长度超出");
            }  
        }else{
        	throw new AppBizException(2009, "commID格式错误");
        }
        return rs;  
    } 
    
    /** 
     * 验证用户名,不支持中文，支持英文（包括全角字符）、数字、下划线和减号 ,长度为36位
     *  
     * @param userName 
     * @return 
     * @throws AppBizException 
     */  
    public static boolean validatePlarcardID(String plarcardID) throws AppBizException {  
        String validateStr = "[A-Za-z0-9-]+";  
        boolean rs = false;  
        rs = matcher(validateStr, plarcardID);  
        if (rs) {  
            int strLenth = getStrLength(plarcardID);  
            if (strLenth < 36 ) {  
            	throw new AppBizException(7, "输入参数plarcardID长度不足");
            }  
            if( strLenth > 36){
            	throw new AppBizException(6, "输入参数plarcardID长度超出");
            }  
        }else{
        	throw new AppBizException(2009, "plarcardID格式错误");
        }
        return rs;  
    } 
    
    /** 
     * 验证code,不支持中文，支持英文、数字、减号 
     *  
     * @param userName 
     * @return 
     * @throws AppBizException 
     */  
    public static boolean validateCode(String content,String errName) throws AppBizException {  
        String validateStr = "^[A-Za-z0-9-]+$";  
        boolean rs = false;  
        rs = matcher(validateStr, content);  
        if (!rs) {
        	throw new AppBizException(3, errName+"格式错误");
        }
        return rs;  
    } 
    
    /** 
     * 验证经纬度
     *  
     * @param userName 
     * @return 
     * @throws AppBizException 
     */  
    public static boolean validateLngAndLat(String longitude,String latitude) throws AppBizException {  
    	String patternLongitude = "^(-?((180)|(((1[0-7]\\d)|(\\d{1,2}))(\\.\\d+)?)))$";
    	String patternLatitude = "^(-?((90)|((([0-8]\\d)|(\\d{1}))(\\.\\d+)?)))$";
        boolean rs = false;  
        boolean rs1 = false;  
        rs = matcher(patternLongitude, longitude);  
        rs1 = matcher(patternLatitude, latitude);  
        if (!rs) {
        	throw new AppBizException(3, "经度格式错误");
        }
        if (!rs1) {
        	throw new AppBizException(3, "纬度格式错误");
        }
        return rs;  
    } 
    
	/**
	 * 校验输入参数是否全是中文
	 * @param number
	 * @return
	 * @throws AppBizException 
	 */
	public static boolean isCharacter(String trueName) throws AppBizException {
		boolean flag = false;
		try {
			Pattern p = Pattern.compile("[\u4E00-\u9FA5]+");
			Matcher m = p.matcher(trueName);
			flag = m.matches();
		} catch (Exception e) {
			flag = false;
		}
		return flag;
	}
	
    private static boolean matcher(String reg, String string) {  
        boolean tem = false;  
        Pattern pattern = Pattern.compile(reg);  
        Matcher matcher = pattern.matcher(string);  
        tem = matcher.matches();  
        return tem;  
    }
    
    /**
	 * 验证是否有表情
	 * 
	 * @param email
	 * @return
	 * @throws AppBizException 
	 */
	public static boolean checkExpression(String expression) throws AppBizException {
		boolean flag = false;
		try {
			String check = "/\\xf0.../";
			Pattern regex = Pattern.compile(check);
			Matcher matcher = regex.matcher(expression);
			flag = matcher.matches();
			if(!flag){
				throw new AppBizException(6000, "不允许添加表情符号");
			}
		} catch (Exception e) {
			flag = false;
			throw new AppBizException(6000, "不允许添加表情符号");
		}
		return flag;
	}
	
	/**
	 * 判断是不是一个合法的车牌号
	 */
	public static boolean isPlate(String plateNum){
		if(plateNum == null || plateNum.trim().length() == 0){
			return false;
		}
		Pattern p = Pattern.compile("^([\u4e00-\u9fa5]|[A-Z]{2})([0-9]{2}|[A-Z])-([0-9A-Za-z]){5}$");
		Matcher m = p.matcher(plateNum);
		if(m.matches()){
			return true;
		}else{
			return false;
		}
	}
}